const fs = require("fs");
const path = require("path");
function addUser(userinfo) {
  const user = require("./user.json") || [];
  const isExist = user.find(item => item.username === userinfo.username)
  if (isExist) {
    return false
  }
  user.push({ ...userinfo });
  fs.writeFileSync(
    path.resolve(__dirname, "./user.json"),
    JSON.stringify(user),
    "utf8"
  );
  return true
}

function listUsers() {
  const user = require("./user.json") || [];
  console.log(user, "user");
  return user;
}

module.exports = {
  addUser: addUser,
  listUsers: listUsers,
};
